package com.ruoyi.web.controller.module.examine;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.domain.module.examine.ExamineApprove;
import com.ruoyi.system.domain.module.examine.ExamineApproveRegInfo;
import com.ruoyi.system.domain.module.examine.ExamineApproveRegRecord;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.module.examine.IExamineApproveRegInfoService;
import com.ruoyi.system.service.module.examine.IExamineApproveRegRecordService;
import com.ruoyi.system.service.module.examine.IExamineApproveService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 审批申请记录Controller
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Validated
@Api(value = "审批申请记录控制器", tags = {"审批申请记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/approve/reg/record")
public class ExamineApproveRegRecordController extends BaseController {

    private final IExamineApproveRegRecordService examineApproveRegRecordService;
    private final IExamineApproveService examineApproveService;
    private final IWxUserService wxUserService;
    private final IExamineApproveRegInfoService examineApproveRegInfoService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

    /**
     * 查询审批申请记录列表
     */
    @ApiOperation("查询审批申请记录列表")
    @PreAuthorize("@ss.hasPermi('module:record:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamineApproveRegRecord> list(@Validated ExamineApproveRegRecord examineApproveRegRecord) {
        QueryWrapper<ExamineApproveRegRecord> qw = new QueryWrapper<>();
        if (examineApproveRegRecord.getApproveId()!=null){
            qw.eq("approve_id",examineApproveRegRecord.getApproveId());
        }
		if (examineApproveRegRecord.getState()!=null){
			qw.eq("state",examineApproveRegRecord.getState());
		}
        TableDataInfo<ExamineApproveRegRecord> td = PageUtils.buildDataInfo(examineApproveRegRecordService.page(PageUtils.buildPage(),qw));
        List<ExamineApproveRegRecord> list = td.getRows();
        for(ExamineApproveRegRecord list1:list){
            if (list1.getApproveId()!=null){
                ExamineApprove approve=examineApproveService.findById(list1.getApproveId());
                if (approve!=null){
                    list1.setApproveName(approve.getApproveTitle()) ;
                }
            }
            if (list1.getUserId()!=null){
                WxUser wxUser=wxUserService.findById(list1.getUserId());
                if (wxUser!=null){
                    list1.setNickName(wxUser.getNickName());
					list1.setFaceUrl(wxUser.getFaceUrl());
					if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && wxUser.getFaceUrl().indexOf("https") < 0) {
						list1.setFaceUrl(imgUrl + wxUser.getFaceUrl());
					}
                }
            }
        }
        return td;
    }

    /**
     * 导出审批申请记录列表
     */
    @ApiOperation("导出审批申请记录列表")
    @PreAuthorize("@ss.hasPermi('module:record:export')")
    @Log(title = "审批申请记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ExamineApproveRegRecord> export(@Validated ExamineApproveRegRecord examineApproveRegRecord) {
        List<ExamineApproveRegRecord> list3 = new ArrayList<>();
//        QueryWrapper<ExamineApprove> qw=new QueryWrapper<>();
//        if (examineApproveRegRecord.getPropertyId()!=null){
//            qw.eq("id",examineApproveRegRecord.getId());
//        }
//        List<ExamineApprove> approve=examineApproveService.list(qw);
//        for (ExamineApprove list1:approve){
            QueryWrapper<ExamineApproveRegRecord> qw1=new QueryWrapper<>();
            if (examineApproveRegRecord.getId()!=null){
                qw1.eq("approve_id",examineApproveRegRecord.getId());
            }
            List<ExamineApproveRegRecord> list2 = examineApproveRegRecordService.list(qw1);
            for (ExamineApproveRegRecord record:list2){
                if (record.getId()!=null){
					QueryWrapper<ExamineApproveRegInfo> qw3 = new QueryWrapper<>();
					qw3.eq("approve_record_id",record.getId());
                    List<ExamineApproveRegInfo> info=examineApproveRegInfoService.list(qw3);
                    if (info!=null){
						record.setGoodName(info.get(0).getGoodName());
						record.setCount(info.get(0).getCount());
						record.setSinglePrice(info.get(0).getSinglePrice());
						record.setTotalPrice(info.get(0).getTotalPrice());
						record.setSpecs(info.get(0).getSpecs());
                    }
                }
                if (record.getUserId()!=null){
                    WxUser wxUser =wxUserService.findById(record.getUserId());
                    if (wxUser!=null){
                        record.setNickName(wxUser.getNickName());
                    }
                }
                System.out.println("报销类型："+record.getApproveType());
                if (record.getApproveType()!=null){

                    if (record.getApproveType()==0){
                        record.setApproveTypeName("报销类");
                    }
                    if (record.getApproveType()==1){
                        record.setApproveTypeName("采购类");
                    }
                }
                if (record.getApproveId()!=null){
                    ExamineApprove approve1=examineApproveService.findById(record.getApproveId());
                    if (approve1!=null){
                        record.setApproveName(approve1.getApproveTitle()) ;
                    }
                }
                list3.add(record);
//            }
        }
        ExcelUtil<ExamineApproveRegRecord> util = new ExcelUtil<ExamineApproveRegRecord>(ExamineApproveRegRecord.class);
        return util.exportExcel(list3, "审批申请记录");
    }

    /**
     * 获取审批申请记录详细信息
     */
    @ApiOperation("获取审批申请记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:record:query')")
    @GetMapping("/{id}")
    public AjaxResult<ExamineApproveRegRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ExamineApproveRegRecord examineApproveRegRecord = examineApproveRegRecordService.getById(id);
        if (examineApproveRegRecord.getApproveId()!=null){
            ExamineApprove approve=examineApproveService.findById(examineApproveRegRecord.getApproveId());
            if (approve!=null){
                examineApproveRegRecord.setApproveName(approve.getApproveTitle()) ;
            }
        }
        if (examineApproveRegRecord.getUserId()!=null){
            WxUser wxUser=wxUserService.findById(examineApproveRegRecord.getUserId());
            if (wxUser!=null){
                examineApproveRegRecord.setNickName(wxUser.getNickName());

				examineApproveRegRecord.setFaceUrl(wxUser.getFaceUrl());
				if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && wxUser.getFaceUrl().indexOf("https") < 0) {
					examineApproveRegRecord.setFaceUrl(imgUrl + wxUser.getFaceUrl());
				}
            }
        }
        return AjaxResult.success(examineApproveRegRecord);
    }

    /**
     * 新增审批申请记录
     */
    @ApiOperation("新增审批申请记录")
    @PreAuthorize("@ss.hasPermi('module:record:add')")
    @Log(title = "审批申请记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ExamineApproveRegRecord examineApproveRegRecord) {
        return toAjax(examineApproveRegRecordService.save(examineApproveRegRecord) ? 1 : 0);
    }

    /**
     * 修改审批申请记录
     */
    @ApiOperation("修改审批申请记录")
    @PreAuthorize("@ss.hasPermi('module:record:edit')")
    @Log(title = "审批申请记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ExamineApproveRegRecord examineApproveRegRecord) {
        return toAjax(examineApproveRegRecordService.updateById(examineApproveRegRecord) ? 1 : 0);
    }

    /**
     * 删除审批申请记录
     */
    @ApiOperation("删除审批申请记录")
    @PreAuthorize("@ss.hasPermi('module:record:remove')")
    @Log(title = "审批申请记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(examineApproveRegRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
